﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Threading

Partial Public Class SnapShot
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim url As String = (IIf((String.IsNullOrEmpty(Request.Params("site"))), "www.google.com", Request.Params("site")))
        Dim width As Integer = (IIf((String.IsNullOrEmpty(Request.Params("width"))), 200, Integer.Parse(Request.Params("width"))))
        Dim height As Integer = (IIf((String.IsNullOrEmpty(Request.Params("height"))), 140, Integer.Parse(Request.Params("height"))))
        Dim capWidth As Integer = (IIf((String.IsNullOrEmpty(Request.Params("capWidth"))), 800, Integer.Parse(Request.Params("capWidth"))))
        Dim capHeight As Integer = (IIf((String.IsNullOrEmpty(Request.Params("capHeight"))), 520, Integer.Parse(Request.Params("capHeight"))))

        'Dim width As Integer = 200
        ' Dim height As Integer = 140
        ' Dim capWidth As Integer = 800
        ' Dim capHeight As Integer = 600

        Dim address As String = "http://" + url
        Dim thumbnail As Bitmap = WebsiteThumbnailImageGenerator.GetWebSiteThumbnail(address, capWidth, capHeight, width, height)

        Response.ContentType = "image/jpeg"
        thumbnail.Save(Response.OutputStream, ImageFormat.Jpeg)
        thumbnail.Dispose()
    End Sub
End Class

Public Class WebsiteThumbnailImageGenerator
    Public Shared Function GetWebSiteThumbnail(ByVal Url As String, ByVal BrowserWidth As Integer, ByVal BrowserHeight As Integer, ByVal ThumbnailWidth As Integer, ByVal ThumbnailHeight As Integer) As Bitmap
        Dim thumbnailGenerator As New WebsiteThumbnailImage(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight)
        Return thumbnailGenerator.GenerateWebSiteThumbnailImage()
    End Function

    Private Class WebsiteThumbnailImage
        Public Sub New(ByVal Url As String, ByVal BrowserWidth As Integer, ByVal BrowserHeight As Integer, ByVal ThumbnailWidth As Integer, ByVal ThumbnailHeight As Integer)
            Me.m_Url = Url
            Me.m_BrowserWidth = BrowserWidth
            Me.m_BrowserHeight = BrowserHeight
            Me.m_ThumbnailHeight = ThumbnailHeight
            Me.m_ThumbnailWidth = ThumbnailWidth
        End Sub

        Private m_Url As String = Nothing
        Public Property Url() As String
            Get
                Return m_Url
            End Get
            Set(ByVal value As String)
                m_Url = value
            End Set
        End Property

        Private m_Bitmap As Bitmap = Nothing
        Public ReadOnly Property ThumbnailImage() As Bitmap
            Get
                Return m_Bitmap
            End Get
        End Property

        Private m_ThumbnailWidth As Integer
        Public Property ThumbnailWidth() As Integer
            Get
                Return m_ThumbnailWidth
            End Get
            Set(ByVal value As Integer)
                m_ThumbnailWidth = value
            End Set
        End Property

        Private m_ThumbnailHeight As Integer
        Public Property ThumbnailHeight() As Integer
            Get
                Return m_ThumbnailHeight
            End Get
            Set(ByVal value As Integer)
                m_ThumbnailHeight = value
            End Set
        End Property

        Private m_BrowserWidth As Integer
        Public Property BrowserWidth() As Integer
            Get
                Return m_BrowserWidth
            End Get
            Set(ByVal value As Integer)
                m_BrowserWidth = value
            End Set
        End Property

        Private m_BrowserHeight As Integer
        Public Property BrowserHeight() As Integer
            Get
                Return m_BrowserHeight
            End Get
            Set(ByVal value As Integer)
                m_BrowserHeight = value
            End Set
        End Property

        Public Function GenerateWebSiteThumbnailImage() As Bitmap
            Dim m_thread As New Thread(New ThreadStart(AddressOf _GenerateWebSiteThumbnailImage))
            m_thread.SetApartmentState(ApartmentState.STA)
            m_thread.Start()
            m_thread.Join()
            Return m_Bitmap
        End Function

        Private Sub _GenerateWebSiteThumbnailImage()
            Dim m_WebBrowser As New WebBrowser()
            m_WebBrowser.ScrollBarsEnabled = False
            m_WebBrowser.Navigate(m_Url)
            AddHandler m_WebBrowser.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted
            While m_WebBrowser.ReadyState <> WebBrowserReadyState.Complete
                Application.DoEvents()
            End While
            m_WebBrowser.Dispose()
        End Sub

        Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
            Dim m_WebBrowser As WebBrowser = DirectCast(sender, WebBrowser)
            m_WebBrowser.ClientSize = New Size(Me.m_BrowserWidth, Me.m_BrowserHeight)
            m_WebBrowser.ScrollBarsEnabled = False
            m_Bitmap = New Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height)
            m_WebBrowser.BringToFront()
            m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds)
            m_Bitmap = DirectCast(m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, Nothing, IntPtr.Zero), Bitmap)
        End Sub
    End Class
End Class